<?php
namespace Modules\Wx\Http\Controllers;

use App\Http\Controllers\Controller;
use App\services\Wechatservice;
use Illuminate\Http\Request;
use Modules\Wx\Entities\Wechat;
use Modules\Wx\Http\Requests\WechatRequest;
class WechatController extends Controller
{
    //显示列表
    public function index()
    {
        $data = Wechat::paginate(10);
        return view('wx::wechat.index', compact('data'));
    }

    //创建视图
    public function create(Wechat $wechat)
    {
        return view('wx::wechat.create',compact('wechat'));
    }

    //保存数据
    public function store(WechatRequest $request,Wechat $wechat)
    {
//        dd(123);
        $post = $request->all();
//        $post['data'] = json_decode($post['data'],true);
//        dd($post);
        $wechat->fill($post);
        $wechat->save();
        return redirect('/wx/wechat')->with('success', '保存成功');
    }

    //显示记录
    public function show(Wechat $field)
    {
        return view('wx::wechat.show', compact('field'));
    }

    //编辑视图
    public function edit(Wechat $wechat)
    {
//        dd(123);
        return view('wx::wechat.edit', compact('wechat'));
    }

    //更新数据
    public function update(WechatRequest $request, Wechat $wechat)
    {
//        dd(123);
        $wechat->update($request->all());
        return redirect('/wx/wechat')->with('success','更新成功');
    }

    //删除模型
    public function destroy(Wechat $wechat)
    {
        $wechat->delete();
        return redirect('wx/wechat')->with('success','删除成功');
    }


//    再写一个方法推送到微信
    public function push(Wechat $wechat,Wechatservice $wechatservice){
//        dd(123);
        $res = $wechatservice->instance('button')->create(['button' => json_decode($wechat['data']),true]);
//        $res = WeChat::instance('button')->create(['button' => $wx_menu['data']]);
        if ($res['errcode'] == 0){
            return back()->with('success','自定义菜单推送到微信成功,请打开手机微信公众号查看');
        }
        return back()->with('danger',$res['errmsg']);
    }
}
